package com.grsu.TransportSystem.dao.impl;

import java.util.ArrayList;
import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.criterion.Restrictions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import com.grsu.TransportSystem.dao.IProposalDAO;
import com.grsu.TransportSystem.domain.Person;
import com.grsu.TransportSystem.domain.Proposal;

@Repository
public class ProposalDAOImpl implements IProposalDAO {

	@Autowired
	private SessionFactory sessionFactory;

	@Override
	public void create(Proposal object) {
		Session session = sessionFactory.getCurrentSession();
		session.save(object);
		
	}
	
	@Override
	public List<Proposal> readByDriver(Person object) {
		List<Proposal> list = null;
		Session session = sessionFactory.getCurrentSession();
		Query query = session.createQuery("from Proposal where personal_id = :driverId ");
		query.setParameter("driverId", object.getId());
		list = query.list();
		return list;
	}
	
	@SuppressWarnings("unchecked")
	@Override
	public List<Proposal> read(Proposal object) {
		List<Proposal> list = null;
		Session session = sessionFactory.getCurrentSession();
		Criteria criteria = session.createCriteria(Proposal.class);
		if(object.getId() != null)
			criteria.add(Restrictions.eq("id", object.getId()));
		if(object.getName() != null)
			criteria.add(Restrictions.eq("name", object.getName()));
		if(object.getDescription() != null)
			criteria.add(Restrictions.eq("description", object.getDescription()));
		if(object.getTo() != null)
			criteria.add(Restrictions.eq("to", object.getTo()));
		if(object.getState() != null)
			criteria.add(Restrictions.eq("state", object.getState()));
		if(object.getDriver() != null)
			criteria.add(Restrictions.eq("driver", object.getDriver()));
		if(object.getCost() != null)
			criteria.add(Restrictions.eq("cost", object.getCost()));
		list = (ArrayList<Proposal>) criteria.list();
		return list;
	}

	@Override
	public void update(Proposal object) {
		Session session = sessionFactory.getCurrentSession();
		session.update(object);
	}
	
	@Override
	public void updateDriver(Proposal object) {
		Session session = sessionFactory.getCurrentSession();
		Query query = session.createQuery("update Proposal set personal_id = :personal_id , state = :state" +
				" where id = :id");
				query.setParameter("id", object.getId());
				query.setParameter("state", object.getState());
				if(object.getDriver()!= null)
					query.setParameter("personal_id", object.getDriver().getId());
				int result = query.executeUpdate();
	}
	
	@Override
	public void updateState(Proposal object) {
		Session session = sessionFactory.getCurrentSession();
		Query query = session.createQuery("update Proposal set state = :state" +
				" where id = :id");
				query.setParameter("id", object.getId());
				query.setParameter("state", object.getState());
				int result = query.executeUpdate();
	}

	@Override
	public void delete(Proposal object) {
		Session session = sessionFactory.getCurrentSession();
		if(object.getId() != null)
		{
		Query q = session.createQuery("delete Proposal where id = "+object.getId());
		q.executeUpdate();
		}
		else
		{throw new RuntimeException();}
	}

	

}
